Skip to content

Check for wp_cache_flush_group() before calling it - #129

Merged
swissspidy merged 1 commit into
mainfrom
fix/phpstan-wp-compat
Aug 26, 2026
Merged

Check for wp_cache_flush_group() before calling it#129
swissspidy merged 1 commit into
mainfrom
fix/phpstan-wp-compat

Conversation

@swissspidy

@swissspidy swissspidy commented Aug 26, 2026

Copy link
Copy Markdown
Member

The johnbillion/wp-compat PHPStan extension that now ships with wp-cli-tests reported a single error for this package:

src/Cache_Command.php:432  wp_cache_flush_group() is only available since WordPress version 6.1.0.

wp cache flush-group guarded that call like this:

if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
    WP_CLI::error( 'Group flushing is not supported.' );
}

if ( ! wp_cache_flush_group( $group ) ) {

wp_cache_supports() and wp_cache_flush_group() both arrived in WordPress 6.1, so the version question was already covered and this is not a compatibility bug against the 4.9 baseline. But the guard tests a different function than the one being called, and that gap is not purely theoretical: wp_cache_supports() is answered by the object cache drop-in, and a drop-in that reports flush_group support without defining wp_cache_flush_group() would produce a fatal instead of the intended error message.

So rather than adding an ignore entry, the condition now also checks for the function it actually calls. That closes the drop-in gap and, since function_exists() is a guard wp-compat understands, resolves the error at the same time — this package needs no wp-compat ignores at all.

Behaviour

Unchanged everywhere it can be observed. On WordPress 6.1+ the function exists, so the added check is always true and the guard behaves exactly as before. Below 6.1 the command already errored with the same message. The difference only shows up for a drop-in that advertises support it does not implement, where a fatal becomes Error: Group flushing is not supported.

Tests

No changes. The existing flush-group scenarios in features/cache.feature are tagged @require-wp-6.1 and continue to pass unaffected, since the added condition is always satisfied there.

Verification

Run locally against the same dependency versions CI resolves (johnbillion/wp-compat 2.0.0, php-stubs/wordpress-stubs v6.9.4, wp-cli/wp-cli-tests v5.2.3):

  • composer phpstan[OK] No errors
  • composer phpcs — clean
  • php -l — clean

GitHub Actions was failing to allocate runners across the org while this was written, so CI may need a re-run once that clears.


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility with environments that do not provide group cache-flushing support.
    • Returns the existing error instead of attempting an unavailable cache operation.

`wp cache flush-group` asked `wp_cache_supports( 'flush_group' )` whether group
flushing was available, then called `wp_cache_flush_group()`. Both arrived in
WordPress 6.1, so the version question was covered, but the guard tested a
different function than the one being called: `wp_cache_supports()` is answered
by the object cache drop-in, and a drop-in that reports support without defining
`wp_cache_flush_group()` would have caused a fatal rather than the intended
error message.

Check for the function itself as well. That is also the guard
johnbillion/wp-compat understands, so the single error it reported for this
package is resolved without needing an ignore entry.

Co-authored-by: Pascal Birchler <[email protected]>
Copilot AI lite review requested due to automatic review settings August 26, 2026 06:22
@swissspidy
swissspidy requested a review from a team as a code owner August 26, 2026 06:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e360afd4-ae6e-48e4-bd23-b3a1a8e18e35

📥 Commits

Reviewing files that changed from the base of the PR and between 0e88bc3 and 3e42184.

📒 Files selected for processing (1)
  • src/Cache_Command.php

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

flush_group now checks that wp_cache_flush_group() exists, in addition to checking cache feature support, before it attempts to flush a cache group.

Changes

Cache group flushing

Layer / File(s) Summary
Validate cache group flush availability
src/Cache_Command.php
flush_group now checks both wp_cache_supports() and the availability of wp_cache_flush_group() before flushing. Unsupported environments use the existing error path.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Merge Risk: ⚪ Minimal · up to 3e421

This localized change prevents a fatal error when cache support is advertised without the required function and preserves existing behavior otherwise; no actionable merge-blocking risk remains.

Suggested reviewers: brianhenryie

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: checking that wp_cache_flush_group() exists before calling it.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/phpstan-wp-compat

Warning

Some tools did not complete. Review the errors below.

🔧 PHPStan (2.2.8)

Invalid configuration:
Unexpected item 'parameters › strictRules'.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added bug command:cache-flush-group Related to 'cache flush-group' command labels Aug 26, 2026
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy added this to the 3.0.1 milestone Aug 26, 2026
@swissspidy
swissspidy merged commit a1dda19 into main Aug 26, 2026
57 checks passed
@swissspidy
swissspidy deleted the fix/phpstan-wp-compat branch August 26, 2026 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:cache-flush-group Related to 'cache flush-group' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants